﻿Public Class QL_SanPham
    Dim cn As New DatabaseConnect
    Dim load_all As String = "Select MaNhap[ma], ngayNhap[ngay], MaNV[nv], COUNT(Id) as solan, NULL[tongtien] From SanPham_Nhap Group By MaNhap, ngayNhap, MaNV union all (Select MaXuat[ma], ngayXuat[ngay], MaNV[nv], COUNT(Id) as solan, tongtien[tongtien] From SanPham_Xuat Group By MaXuat, ngayXuat, MaNV, tongtien)"

    Private Sub LoadDGV(ByVal _loadDGV As String)
        'Load dgv
        Dim dt As New DataTable
        dt = cn.GetData(_loadDGV)
        dgv.DataSource = dt
    End Sub
    Private Sub QL_SanPham_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDGV(load_all)
        Dim dt As New DataTable
        dt = cn.GetData(load_all)
        cbNgayThang.DataSource = dt
        cbNgayThang.DisplayMember = "ngay"
        cbNgayThang.ValueMember = "ngay"
    End Sub

    Private Sub ThoátToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThoátToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub KếtNốiDữLiệuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KếtNốiDữLiệuToolStripMenuItem.Click
        If Main.flag_login = 0 Then
            MessageBox.Show("Chưa đăng nhập không thể kết nối")
            Dim dn As New DangNhap()
            dn.ShowDialog()
            If Main.flag_login = 1 Then
                MessageBox.Show("Đã kết nối tới Cơ sở dữ liệu")
                KếtNốiDữLiệuToolStripMenuItem.Text = "Đã kết nối!"
                KếtNốiDữLiệuToolStripMenuItem.Enabled = False
                NgắtKếtNốiDữLiệuToolStripMenuItem.Enabled = True
                Dim dt As New DataTable
                dt = cn.GetData("select * from SanPham")
                cbMaLoai.DataSource = dt
                cbMaLoai.DisplayMember = "tenLoai"
                cbMaLoai.ValueMember = "MaLoai"
            End If
            Return
        Else
            KếtNốiDữLiệuToolStripMenuItem.Text = "Đã kết nối!"
            KếtNốiDữLiệuToolStripMenuItem.Enabled = False
            NgắtKếtNốiDữLiệuToolStripMenuItem.Enabled = True
            Dim dt As New DataTable
            dt = cn.GetData("select * from SanPham")
            cbMaLoai.DataSource = dt
            cbMaLoai.DisplayMember = "tenLoai"
            cbMaLoai.ValueMember = "MaLoai"
        End If
    End Sub

    Private Sub NgắtKếtNốiDữLiệuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NgắtKếtNốiDữLiệuToolStripMenuItem.Click
        Dim ngatketnoi As New QuanLyNam.Bus
        ngatketnoi.connection.Close()
        MessageBox.Show("Đã ngắt kết nối tới Cơ sở dữ liệu")
        KếtNốiDữLiệuToolStripMenuItem.Text = "Kết nối dữ liệu"
        KếtNốiDữLiệuToolStripMenuItem.Enabled = True
        NgắtKếtNốiDữLiệuToolStripMenuItem.Enabled = False
        Dim dt As New DataTable
        cbMaLoai.DataSource = dt
        cbNgayThang.DataSource = dt
        dgv.DataSource = dt
    End Sub

    Private Sub txtTim_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTim.TextChanged
        'Tim kiem khi nhap chu
        If txtTim.Text <> "" Then
            LoadDGV("SELECT * FROM (" + load_all + ") as load_all where load_all.ghichu like N'%" + txtTim.Text + "%'")
        End If
    End Sub

    Private Sub cbMaLoai_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbMaLoai.SelectedIndexChanged
        If cbMaLoai.SelectedIndex <> 0 Then
            lbDVT.Visible = True
            lbTrigia.Visible = True
            lbTinhtrang.Visible = True
            Dim dt As New DataTable
            Dim cn As New DatabaseConnect
            dt = cn.GetData("select dvt.donvitinh,tg.trigia,tt.tinhtrang from SanPham sp,SanPham_DonViTinh dvt,SanPham_TriGia tg, SanPham_TinhTrang tt where sp.donvitinh = dvt.donvitinhID and sp.trigia=tg.trigiaID and sp.tinhtrang = tt.tinhtrangID and sp.MaLoai =" & cbMaLoai.SelectedValue & "")
            lbDVT.Text = dt.Rows(0)("donvitinh").ToString()
            lbTrigia.Text = dt.Rows(0)("trigia").ToString()
            lbTinhtrang.Text = dt.Rows(0)("tinhtrang").ToString()
        End If
    End Sub

    Private Sub rdTatCa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdTatCa.Click
        LoadDGV(load_all)
    End Sub

    Private Sub rdNhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdNhap.Click
        LoadDGV("select * from SanPham_Nhap")
    End Sub

    Private Sub rdXuat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdXuat.Click
        LoadDGV("select * from SanPham_Xuat")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        LoadDGV("Select * FROM ( " + load_all + ") as load_all WHERE load_all.ngay = '" + Convert.ToDateTime(cbNgayThang.SelectedValue) + "'")
        rdTatCa.Checked = False
        rdNhap.Checked = False
        rdXuat.Checked = False
    End Sub

    Private Sub ChiTiet()
        If dgv.SelectedRows.Count > 0 Then
            Dim ma As String = dgv.SelectedRows(0).Cells(0).Value.ToString()
            If ma.IndexOf("N") <> -1 Then
                Dim n As New QL_SP_Nhap()
                Me.Hide()
                n.ma = ma
                n.ShowDialog()
                Me.Show()
            Else
                Dim x As New QL_SP_Xuat()
                Me.Hide()
                x.ma = ma
                x.ShowDialog()
                Me.Show()
            End If
        End If
    End Sub

    Private Sub dgv_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgv.KeyDown
        If dgv.SelectedRows.Count > 0 And e.KeyCode = Keys.Enter Then
            ChiTiet()
        End If
    End Sub

    Private Sub dgv_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgv.DoubleClick
        If dgv.SelectedRows.Count > 0 Then
            ChiTiet()
        End If
    End Sub
End Class
